package com.zrar.ak.gkpt.service.impl.record;

import com.zrar.ak.gkpt.dao.*;

import com.zrar.ak.gkpt.model.bo.*;
import com.zrar.ak.gkpt.model.vo.*;
import com.zrar.ak.gkpt.service.record.IBusinessBidPurchaseService;
import com.zrar.ak.gkpt.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BusinessBidPurchaseSerice
 * @Description 采购中心招标采购Service层
 * @Author HuangKeXin
 * @Time 2020/04/4
 */
@Service
public class BusinessBidPurchaseSerice implements IBusinessBidPurchaseService {
    @Autowired
    private IBusinessBidPurchaseBidDao businessBidPurchaseBidDao;
    @Autowired
    private IBusinessBidPurchasePurchaseSystemSoftwareHardwareDao businessBidPurchasePurchaseSystemSoftwareHardwareDao;
    @Autowired
    private IBusinessBidPurchaseBidTerminalDao terminalDao;
    @Autowired
    private IBusinessBidPurchaseBidSystemDao systemInfoDao;
    @Autowired
    private IBusinessBidPurchaseBidPeripheralDao peripheralInfoDao;
    @Autowired
    private IBusinessBidPurchaseBidServerDao serverInfoDao;
    @Autowired
    private IBusinessBidPurchaseBidEquipmentDao softwareHardwareInfoDao;


    /**
     * 招标基本信息保存
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return
     */
    @Override
    public JsonResult businessBidSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessBidPurchaseBidBO businessBidPurchaseBidBO = new BusinessBidPurchaseBidBO();
        try {
            businessBidPurchaseBidBO =  CommonTools.jsonStrToObject(formDataJson, BusinessBidPurchaseBidBO.class);
            String unitId = businessBidPurchaseBidBO.getUnitId();
            //判断对应的建设单位是否有已存在招标信息，若有则无法执行insert操作，若无可继续执行
            int businessBidPurchaseBidTotal = businessBidPurchaseBidDao.queryExistCountByBusinessBidUnitId(unitId);
            if(businessBidPurchaseBidTotal>0){
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_EXISTENT_DATA);
            }else{
                String creatorId = userVO.getUserId();
                Date createTime = new Date();
                String updateUserId = userVO.getUserName();
                Date updateTime = new Date();
                businessBidPurchaseBidBO.setId(CommonTools.getUUID());
                businessBidPurchaseBidBO.setCreatorId(creatorId);
                businessBidPurchaseBidBO.setCreateTime(createTime);
                businessBidPurchaseBidBO.setUpdateUserId(updateUserId);
                businessBidPurchaseBidBO.setUpdateTime(updateTime);
                businessBidPurchaseBidBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int insert = businessBidPurchaseBidDao.insert(businessBidPurchaseBidBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }
    /**
     * 招标基本信息修改
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return
     */
    @Override
    public JsonResult businessBidUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        BusinessBidPurchaseBidBO businessBidPurchaseBidBO = new BusinessBidPurchaseBidBO();
        String updateUserId = userVO.getUserName();
        Date updateTime = new Date();
        try {
            businessBidPurchaseBidBO = CommonTools.jsonStrToObject(formDataJson, BusinessBidPurchaseBidBO.class);
            BusinessBidPurchaseBidBO businessBidPurchaseBidInfo = businessBidPurchaseBidDao.selectByPrimaryKey(businessBidPurchaseBidBO.getId());

            if(businessBidPurchaseBidInfo==null || BusinessEnum.IS_DELETE_Y.getValue().equals(businessBidPurchaseBidInfo.getIsDelete())){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
            }else if(!businessBidPurchaseBidInfo.getUnitId().equals(businessBidPurchaseBidBO.getUnitId())){
                //如果同一主键的数据库表记录的建设单位编码和传入数据的建设单位编码值不一样，则返回业务异常错误
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }else {
                businessBidPurchaseBidBO.setCreatorId(businessBidPurchaseBidInfo.getCreatorId());
                businessBidPurchaseBidBO.setCreateTime(businessBidPurchaseBidInfo.getCreateTime());
                businessBidPurchaseBidBO.setUpdateUserId(updateUserId);
                businessBidPurchaseBidBO.setUpdateTime(updateTime);
                businessBidPurchaseBidBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
                int i = businessBidPurchaseBidDao.updateByPrimaryKey(businessBidPurchaseBidBO);
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }
    /**
     * 获取招标信息列表(分页模糊查询)
     * @param currentPage
     * @param pageSize
     * @param projectName
     * @return
     */
    /*@Override
    public JsonPageResult getBusinessBidInfoList(JsonPageResult jsonResult, int currentPage, int pageSize, String unitName,String bidWay) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if(StringUtils.isEmpty(unitName)){
            unitName = null;

        }
        if (StringUtils.isEmpty(bidWay)) {
            bidWay = null;
        }
        Map map = new HashMap();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("unitName",unitName);
        map.put("bidWay",bidWay);
        //返回前端的具体的数据
        List list = null;
        //符合展示条件的数据总数
        Integer total = null;
        total = businessBidPurchaseBidDao.queryBusinessBidShowNum(map);
        list = businessBidPurchaseBidDao.pagingQueryBusinessBidList(map);

        if(total==null){
            total=0;
        }
        //计算一共多少页
        Integer pageNum = (total%pageSize==0)?(total/pageSize):(total/pageSize+1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if(list == null || list.size()==0){
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
        }else{
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }*/
    @Override
    public JsonPageResult getBusinessBidInfoList(JsonPageResult jsonResult, int currentPage, int pageSize,String projectName,String bidWay) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if (StringUtils.isEmpty(projectName)) {
            projectName = null;
        }
        if (StringUtils.isEmpty(bidWay)) {
            bidWay = null;
        }
        Map map = new HashMap();
        map.put("currentPage", currentPage);
        map.put("pageSize", pageSize);
        map.put("projectName", projectName);
        map.put("bidWay", bidWay);
        //返回前端的具体的数据
        List list = null;
        //符合展示条件的数据总数
        Integer total = null;
        total = businessBidPurchaseBidDao.queryBusinessBidShowNum(map);
        list = businessBidPurchaseBidDao.pagingQueryBusinessBidList(map);
        if (total == null) {
            total = 0;
        }
        //计算一共多少页
        Integer pageNum = (total % pageSize == 0) ? (total / pageSize) : (total / pageSize + 1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if (list == null || list.size() == 0) {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_NONEXISTENT_DATA);
        } else {
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }
//    /**
//     * 采购信息保存
//     * @param jsonResult
//     * @param formDataJson
//     * @param userVO
//     * @return
//     */
//    @Override
//    public JsonResult businessPurchaseSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
//        BusinessBidPurchasePurchaseSystemSoftwareHardwareBO businessBidPurchasePurchaseSystemSoftwareHardwareBO = new BusinessBidPurchasePurchaseSystemSoftwareHardwareBO();
//        String jsonStr = CommonTools.objectToJsonStr(businessBidPurchasePurchaseSystemSoftwareHardwareBO);
//        FormDataParameter parameter = new FormDataParameter();
//        parameter.setFormDataJson(jsonStr);
//        jsonResult.setData(parameter);
//        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
//        try {
//            businessBidPurchasePurchaseSystemSoftwareHardwareBO =  CommonTools.jsonStrToObject(formDataJson, BusinessBidPurchasePurchaseSystemSoftwareHardwareBO.class);
//            String unitId = businessBidPurchasePurchaseSystemSoftwareHardwareBO.getUnitId();
//            //只有当该建设单位存在招标信息且不存在采购信息时，才能执行采购信息的新增操作
//            int businessBidPurchaseBidTotal = businessBidPurchaseBidDao.queryExistCountByBusinessBidUnitId(unitId);
//            int businessBidPurchaseSystemSoftwareHardwareInfoTotal = businessBidPurchasePurchaseSystemSoftwareHardwareDao.queryExistCountByBusinessBidPurchaseUnitId(unitId);
//            if(businessBidPurchaseBidTotal>0 && businessBidPurchaseBidTotal==0){
//                String creatorId = userVO.getUserId();
//                Date createTime = new Date();
//                String updateUserId = userVO.getUserName();
//                Date updateTime = new Date();
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setId(CommonTools.getUUID());
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setCreatorId(creatorId);
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setCreateTime(createTime);
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setUpdateUserId(updateUserId);
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setUpdateTime(updateTime);
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
//                int insert = businessBidPurchasePurchaseSystemSoftwareHardwareDao.insert(businessBidPurchasePurchaseSystemSoftwareHardwareBO);
//                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
//            }else if(businessBidPurchaseSystemSoftwareHardwareInfoTotal>0){
//                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_EXISTENT_DATA);
//            }else {
//                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
//
//
//        }
//        return jsonResult;
//
//    }
//    /**
//     * 采购信息修改
//     * @param jsonResult
//     * @param formDataJson
//     * @param userVO
//     * @return
//     */
//    @Override
//    public JsonResult businessPurchaseUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {
//        BusinessBidPurchasePurchaseSystemSoftwareHardwareBO businessBidPurchasePurchaseSystemSoftwareHardwareBO = new BusinessBidPurchasePurchaseSystemSoftwareHardwareBO();
//        String updateUserId = userVO.getUserName();
//        Date updateTime = new Date();
//        try {
//            businessBidPurchasePurchaseSystemSoftwareHardwareBO = CommonTools.jsonStrToObject(formDataJson, BusinessBidPurchasePurchaseSystemSoftwareHardwareBO.class);
//            BusinessBidPurchasePurchaseSystemSoftwareHardwareBO businessBidPurchaseSystem = businessBidPurchasePurchaseSystemSoftwareHardwareDao.selectByPrimaryKey(businessBidPurchasePurchaseSystemSoftwareHardwareBO.getId());
//            if(businessBidPurchaseSystem == null || BusinessEnum.IS_DELETE_Y.getValue().equals(businessBidPurchaseSystem.getIsDelete())){
//                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
//            }else if(!businessBidPurchaseSystem.getUnitId().equals(businessBidPurchasePurchaseSystemSoftwareHardwareBO.getUnitId())){
//                //如果同一主键的数据库表记录的建设单位编码和传入数据的建设单位编码值不一样，则返回业务异常错误
//                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
//            }else{
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setCreatorId(businessBidPurchaseSystem.getCreatorId());
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setCreateTime(businessBidPurchaseSystem.getCreateTime());
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setUpdateUserId(updateUserId);
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setUpdateTime(updateTime);
//                businessBidPurchasePurchaseSystemSoftwareHardwareBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
//                int i = businessBidPurchasePurchaseSystemSoftwareHardwareDao.updateByPrimaryKey(businessBidPurchasePurchaseSystemSoftwareHardwareBO);
//                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
//        }
//        return jsonResult;
//    }
    /**
     * 根据建设单位编码将对应的招标信息与对应的采购信息置为无效
     * @param jsonResult
     * @param unitId
     * @param bidType :招标处理类型(1:招标基本信息和采购信息执行删除操作 2:采购信息执行删除操作)
     * @return
     */
    @Override
    public JsonResult deleteBusinessBidInfoByUnitId(JsonResult jsonResult, String unitId, String bidType) {
        /**
         * bidType :招标处理类型(1:招标基本信息和采购信息执行删除操作 2:采购信息执行删除操作)
         */
        if("1".equals(bidType)){
            businessBidPurchaseBidDao.deleteBusinessBidByUnitId(unitId);
            businessBidPurchasePurchaseSystemSoftwareHardwareDao.deleteBusinessBidPurchaseByUnitId(unitId);
        }else if("2".equals(bidType)){
            businessBidPurchasePurchaseSystemSoftwareHardwareDao.deleteBusinessBidPurchaseByUnitId(unitId);
        }
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }
    /**
     * 根据建设单位编码获取招标信息与对应的采购信息
     * @param unitId
     * @param jsonResult
     * @return
     */
    @Override
    public JsonResult businessBidInfoGetByUnitId(JsonResult jsonResult, String unitId) {
        Map map = new HashMap();
        BusinessBidPurchaseBidBO businessBidPurchaseBidBO = new BusinessBidPurchaseBidBO();
        businessBidPurchaseBidBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        businessBidPurchaseBidBO.setUnitId(unitId);
        BusinessBidPurchasePurchaseSystemSoftwareHardwareBO businessBidPurchasePurchaseSystemSoftwareHardwareBO = new BusinessBidPurchasePurchaseSystemSoftwareHardwareBO();
        businessBidPurchasePurchaseSystemSoftwareHardwareBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        businessBidPurchasePurchaseSystemSoftwareHardwareBO.setUnitId(unitId);
        businessBidPurchaseBidBO = businessBidPurchaseBidDao.selectOne(businessBidPurchaseBidBO);
        businessBidPurchasePurchaseSystemSoftwareHardwareBO = businessBidPurchasePurchaseSystemSoftwareHardwareDao.selectOne(businessBidPurchasePurchaseSystemSoftwareHardwareBO);
        map.put("businessBidPurchaseBidBO",businessBidPurchaseBidBO);
        map.put("businessBidPurchasePurchaseSystemSoftwareHardwareBO",businessBidPurchasePurchaseSystemSoftwareHardwareBO);
        jsonResult.setData(map);
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }
















    /**
     * 采购信息保存
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return
     */
    @Override
    public JsonResult purchaseInfoSave(JsonResult jsonResult, String formDataJson, UserVO userVO) {
        Map map = new HashMap();
        int queryCount = 0;
        BusinessPurchaseContainerVO businessPurchaseContainerVO = new BusinessPurchaseContainerVO();
        // 将json数据转换为SchemeContainerVO封装类
        try {
            businessPurchaseContainerVO = CommonTools.jsonStrToObject(formDataJson, BusinessPurchaseContainerVO.class);
            //判断插入的数据是否存在
            List<BusinessBidPurchaseBidPeripheralBO> peripheralPurchaseList = businessPurchaseContainerVO.getPurchasePeripheral();
            List<BusinessBidPurchaseBidServerBO> serverPurchaseList = businessPurchaseContainerVO.getPurchaseServer();
            List<BusinessBidPurchaseBidEquipmentBO> softPurchaseList = businessPurchaseContainerVO.getPurchaseSoft();
            List<BusinessBidPurchaseBidSystemBO> systemPurchaseList = businessPurchaseContainerVO.getPurchaseSystem();
            List<BusinessBidPurchaseBidTerminalBO> terminalPurchaseList = businessPurchaseContainerVO.getPurchaseTerminal();
            // 记录查询条数
            // 判断外设是否已经存在数据

            for (BusinessBidPurchaseBidPeripheralBO per : peripheralPurchaseList) {
                // 终端类型
                String peripheralTerminalType = per.getTerminalType();
                // 外设类型
                String peripheralType = per.getPeripheralType();

                String typeSpecification = per.getTypeSpecification();

                Integer amount = per.getAmount();

                String brandManufactorName = per.getBrandManufactorName();

                // 存入外设表主键
                per.setId(CommonTools.getUUID());
                // 基础信息主键
                String basicsId = per.getBasicsId();



                map.put("basicsId", basicsId);
                map.put("peripheralTerminalType", peripheralTerminalType);
                map.put("peripheralType", peripheralType);
                map.put("amount", amount);
                map.put("brandManufactorName", brandManufactorName);
                map.put("typeSpecification", typeSpecification);



                // 根据终端类型、外设类型和基础信息主键查询，有数据不允许插入
                queryCount = peripheralInfoDao.queryByType(map) + queryCount;
            }
            // 判断服务是否已经存在数据
            for (BusinessBidPurchaseBidServerBO server : serverPurchaseList) {

                String serverType = server.getServerType();
                String serverBrandManufactorName = server.getBrandManufactorName();
                Integer serverAmount = server.getAmount();
                String serverTypeSpecification = server.getTypeSpecification();


                server.setId(CommonTools.getUUID());

                String basicsId = server.getBasicsId();
                map.put("basicsId", basicsId);
                map.put("serverType", serverType);
                map.put("serverBrandManufactorName", serverBrandManufactorName);
                map.put("serverAmount", serverAmount);
                map.put("serverTypeSpecification", serverTypeSpecification);
                // 有数据不允许插入
                queryCount = serverInfoDao.queryByType(map) + queryCount;



            }
            // 判断软硬件信息是否已经存在
            for (BusinessBidPurchaseBidEquipmentBO soft : softPurchaseList) {


                String equipmentType = soft.getEquipmentType();
                // 终端/服务器类型位置
                String brandManufactorName = soft.getBrandManufactorName();

                String terminalType = soft.getTerminalType();

                Integer amount = soft.getAmount();

                String typeSpecification = soft.getTypeSpecification();




                // 存入硬件表主键
                soft.setId(CommonTools.getUUID());
                // 基础信息主键
                String basicsId = soft.getBasicsId();
                map.put("basicsId", basicsId);
                map.put("equipmentType", equipmentType);
                map.put("brandManufactorName", brandManufactorName);
                map.put("terminalType", terminalType);
                map.put("amount", amount);
                map.put("typeSpecification", typeSpecification);
                // 有数据不允许插入
                queryCount = softwareHardwareInfoDao.queryByType(map) + queryCount;
            }
            // 判断系统信息是否已经存在
            for (BusinessBidPurchaseBidSystemBO system : systemPurchaseList) {




                String systemName = system.getSystemName();
                String model = system.getModel();
                String brandManufactorName = system.getBrandManufactorName();
                String typeSpecification = system.getTypeSpecification();




                // 存入系统表主键
                system.setId(CommonTools.getUUID());
                // 基础信息主键
                String basicsId = system.getBasicsId();



                map.put("basicsId", basicsId);
                map.put("systemName", systemName);
                map.put("model", model);
                map.put("brandManufactorName", brandManufactorName);
                map.put("typeSpecification", typeSpecification);



                // 有数据不允许插入
                queryCount = systemInfoDao.queryByType(map) + queryCount;
            }
            // 判断终端信息是否已经存在
            for (BusinessBidPurchaseBidTerminalBO terminal : terminalPurchaseList) {



                String terminalSystemName = terminal.getBrandManufactorName();
                String terminalType = terminal.getTerminalType();
                Integer amount = terminal.getAmount();
                String typeSpecification = terminal.getTypeSpecification();


                // 存入终端表主键
                terminal.setId(CommonTools.getUUID());
                // 获取终端表中的基本信息表主键
                String basicsId = terminal.getBasicsId();



                map.put("basicsId", basicsId);
                map.put("terminalSystemName", terminalSystemName);
                map.put("terminalType", terminalType);
                map.put("amount", amount);
                map.put("typeSpecification", typeSpecification);

                // 有数据不允许插入
                queryCount = terminalDao.queryByType(map)+ queryCount;
            }
            if (queryCount > 0) {
                // 数据已存在，返会信息
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_EXISTENT_DATA);
            } else {
                terminalDao.insertInfo(terminalPurchaseList);
                systemInfoDao.insertInfo(systemPurchaseList);
                peripheralInfoDao.insertInfo(peripheralPurchaseList);
                serverInfoDao.insertInfo(serverPurchaseList);
                softwareHardwareInfoDao.insertInfo(softPurchaseList);
                CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }
        return jsonResult;
    }



    /**
     * 采购表更新
     *
     * @param jsonResult
     * @param formDataJson
     * @param userVO
     * @return
     */
    @Override
    public JsonResult purchaseInfoUpdate(JsonResult jsonResult, String formDataJson, UserVO userVO) {

        Map map = null;
        BusinessPurchaseContainerVO schemeContainer = new BusinessPurchaseContainerVO();
        BusinessPurchaseUpdateContainerVo businessPurchaseUpdateContainerVo = new BusinessPurchaseUpdateContainerVo();



        // 将json数据转换为SchemeContainerVO封装类
        try {
            businessPurchaseUpdateContainerVo = CommonTools.jsonStrToObject(formDataJson, BusinessPurchaseUpdateContainerVo.class);
            String unitId = businessPurchaseUpdateContainerVo.getUnitId();
            BusinessBidPurchaseBidBO businessBidPurchaseBidBO = businessBidPurchaseBidDao.queryStatusByUnitId(unitId);
            String basicsId = businessBidPurchaseBidBO.getId();
            List<BusinessBidPurchaseBidPeripheralBO> peripheralList = businessPurchaseUpdateContainerVo.getBusinessPurchaseContainerVO().getPurchasePeripheral();
            List<BusinessBidPurchaseBidServerBO> serverList = businessPurchaseUpdateContainerVo.getBusinessPurchaseContainerVO().getPurchaseServer();
            List<BusinessBidPurchaseBidEquipmentBO> softList = businessPurchaseUpdateContainerVo.getBusinessPurchaseContainerVO().getPurchaseSoft();
            List<BusinessBidPurchaseBidSystemBO> systemList = businessPurchaseUpdateContainerVo.getBusinessPurchaseContainerVO().getPurchaseSystem();
            List<BusinessBidPurchaseBidTerminalBO> terminalList = businessPurchaseUpdateContainerVo.getBusinessPurchaseContainerVO().getPurchaseTerminal();
            // 判断接收的数据中基本数据主键和传入建设单位的主键是否相同
            int count = 0;
            for (BusinessBidPurchaseBidPeripheralBO per : peripheralList) {
                if(basicsId == per.getBasicsId()){
                    count++;
                }
            }
            for (BusinessBidPurchaseBidServerBO server : serverList) {
                if(basicsId == server.getBasicsId()){
                    count++;
                }
            }
            for (BusinessBidPurchaseBidEquipmentBO soft : softList) {
                if(basicsId == soft.getBasicsId()){
                    count++;
                }
            }
            for (BusinessBidPurchaseBidSystemBO system : systemList) {
                if(basicsId == system.getBasicsId()){
                    count++;
                }
            }
            for (BusinessBidPurchaseBidTerminalBO terminalInfo : terminalList) {
                if(basicsId == terminalInfo.getBasicsId()){
                    count++;
                }
            }
            if(businessBidPurchaseBidBO == null || BusinessEnum.IS_DELETE_Y.equals(businessBidPurchaseBidBO.getIsDelete())){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
            }else if(count > 0){
                CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_BUSINESS_EXCEPTION);
            }else{
                terminalDao.updateInfo(terminalList);
                systemInfoDao.updateInfo(systemList);
                peripheralInfoDao.updateInfo(peripheralList);
                serverInfoDao.updateInfo(serverList);
                softwareHardwareInfoDao.updateInfo(softList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_BUSINESS_EXCEPTION);
        }


        return jsonResult;
    }



    /**
     * 根据基础信息表中主键id删除采购信息
     * @param basicsId 基础信息主键id
     * @return JsonResult
     */
    @Override
    public JsonResult deletePurchaseByBasicsId(JsonResult jsonResult, String basicsId) {


        BusinessBidPurchaseBidBO basicsBO = new BusinessBidPurchaseBidBO();
        basicsBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
        basicsBO.setId(basicsId);
        businessBidPurchaseBidDao.updateByPrimaryKey(basicsBO);
        jsonResult = CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }


    /**
     * 根据主键id查看采购信息
     *
     * @param basicsId 基础信息主键
     * @return JsonResult
     */
    @Override
    public JsonResult queryPurchaseByUnitId(JsonResult jsonResult, String basicsId) {

        BusinessPurchaseContainerVO businessPurchaseContainerVO = new BusinessPurchaseContainerVO();
        // 查询数据
        List<BusinessBidPurchaseBidTerminalBO> terminalList = terminalDao.queryAllByBasicsId(basicsId);
        List<BusinessBidPurchaseBidSystemBO> systemList = systemInfoDao.queryAllByBasicsId(basicsId);
        List<BusinessBidPurchaseBidPeripheralBO> peripherList = peripheralInfoDao.queryAllByBasicsId(basicsId);
        List<BusinessBidPurchaseBidEquipmentBO> softList = softwareHardwareInfoDao.queryAllByBasicsId(basicsId);
        List<BusinessBidPurchaseBidServerBO> serverList = serverInfoDao.queryAllByBasicsId(basicsId);
        // 封装数据
        businessPurchaseContainerVO.setPurchasePeripheral(peripherList);
        businessPurchaseContainerVO.setPurchaseServer(serverList);
        businessPurchaseContainerVO.setPurchaseSystem(systemList);
        businessPurchaseContainerVO.setPurchaseTerminal(terminalList);
        businessPurchaseContainerVO.setPurchaseSoft(softList);

        jsonResult.setData(businessPurchaseContainerVO);
        CommonTools.jsonResultSetParameter(jsonResult, JsonResultEnum.CODE_SUCCESS);
        return jsonResult;

    }

}
